package com.br.aiops.listeners.ladder;

import com.br.aiops.counter.AlertLogCounter;
import com.br.aiops.counter.CounterLadderValue;

public class DurationOrAlertCountLadderJudge extends AbstractLadderJudge {

    public DurationOrAlertCountLadderJudge(AlertLogCounter alertLogCounter) {
        super(alertLogCounter);
    }

    @Override
    public void judgeLadder() {
        //达到告警次数
        if (counter.getAlertTimes().get() >= CounterLadderValue.getLadderValue(counter.counterKey()).getCurrentTimes().get()) {
            sendAlert();
            //升级次数阈值
            CounterLadderValue.upgradeTimes(counter.counterKey());
        }

        //特殊情况，如果日期在阶梯周期之前，则不统计
        LadderCycleStatus cycleStatus = super.initCycleIfAbsent();
        if (cycleStatus.beforeFirstCycle(counter.getAlertLog().getLogTime())) {
            return;
        }
        //统计该周期
        LadderCycleStatus counterStatus = super.countCycle(cycleStatus);

        if (counterStatus.hasFlag(counter.getAlertLog().getLogTime())) {
            if (counterStatus.getCount(counter.getAlertLog().getLogTime()) > 0) {
                counterStatus.flag(counter.getAlertLog().getLogTime());
                sendAlert();
            }
        }
    }
}
